
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Windows.Forms;
using Fcdbas.Core.Windows;
namespace Fcdbas.Core
{
	[System.Diagnostics.DebuggerDisplay("{Control}")]
	[System.Serializable]
	public struct SalWindowHandle : System.IConvertible, System.IComparable, ISalType, System.Runtime.Serialization.ISerializable
	{
		private class ControlWeakReference
		{
			internal System.Runtime.InteropServices.GCHandle Handle;
			internal ControlWeakReference(System.Windows.Forms.Control ctrl)
			{
				this.Handle = System.Runtime.InteropServices.GCHandle.Alloc(ctrl, System.Runtime.InteropServices.GCHandleType.Weak);
			}
			~ControlWeakReference()
			{
				this.Handle.Free();
			}
		}
		[Browsable(false), System.Diagnostics.DebuggerStepThrough, System.Obsolete("Replaced by SalArray<T>.")]
		[System.Serializable]
		public class Array : SalArray<SalWindowHandle>
		{
			public Array()
			{
			}
			public Array(params int[] elems) : base(elems)
			{
			}
			public Array(params string[] elems) : base(elems)
			{
			}
			protected override object CreateInstance()
			{
				return SalWindowHandle.Null;
			}
		}
		public static readonly SalWindowHandle Null = default(SalWindowHandle);
		private SalWindowHandle.ControlWeakReference m_deref;
		private System.IntPtr m_value;
		public bool IsNull
		{
			[System.Diagnostics.DebuggerStepThrough]
			get
			{
				return this.ToIntPtr() == System.IntPtr.Zero;
			}
		}
		public System.IntPtr Value
		{
			[System.Diagnostics.DebuggerStepThrough]
			get
			{
				return this.ToIntPtr();
			}
		}
		
		DbType ISalType.DbType
		{
			get
			{
				return DbType.UInt32;
			}
		}
		object ISalType.Value
		{
			get
			{
				return this.Value;
			}
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalWindowHandle(System.IntPtr handle)
		{
			this = new SalWindowHandle(handle, true);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalWindowHandle(System.Windows.Forms.Control ctrl)
		{
			this.m_deref = null;
			this.m_value = System.IntPtr.Zero;
			this.InitFromControl(ctrl);
		}
		[System.Diagnostics.DebuggerStepThrough]
		private SalWindowHandle(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
		{
			this.m_deref = null;
			this.m_value = (System.IntPtr)info.GetValue("Handle", typeof(System.IntPtr));
			this.InitFromControl(System.Windows.Forms.Control.FromHandle(this.m_value));
		}
		[System.Diagnostics.DebuggerStepThrough]
		internal SalWindowHandle(System.IntPtr handle, bool deref)
		{
			this.m_deref = null;
			this.m_value = handle;
			if (deref)
			{
				this.InitFromControl(System.Windows.Forms.Control.FromHandle(handle));
			}
		}
		private void InitFromControl(System.Windows.Forms.Control ctrl)
		{
			if (ctrl != null)
			{
				ctrl = SalWindow.GetDerivedInstance(ctrl);
				if (ctrl != null)
				{
					this.m_deref = new SalWindowHandle.ControlWeakReference(ctrl);
					this.m_value = (System.IntPtr)this.m_deref.Handle;
				}
			}
		}
		[System.Diagnostics.DebuggerStepThrough]
		public SalNumber ToNumber()
		{
			return new SalNumber(this.ToIntPtr());
		}
        
		[System.Diagnostics.DebuggerStepThrough]
		public System.IntPtr ToIntPtr()
		{
			if (this.m_deref == null || this.m_value == System.IntPtr.Zero)
			{
				return this.m_value;
			}
			System.Windows.Forms.Control control = this.ToControl();
			if (control != null)
			{
				return control.Handle;
			}
			return System.IntPtr.Zero;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public int ToInt32()
		{
			return this.ToIntPtr().ToInt32();
		}
		
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator System.IntPtr(SalWindowHandle handle)
		{
			return handle.Value;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator System.Windows.Forms.Control(SalWindowHandle handle)
		{
			return handle.ToControl();
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalWindowHandle(System.Windows.Forms.Control ctrl)
		{
			return new SalWindowHandle(ctrl);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static explicit operator int(SalWindowHandle handle)
		{
			return handle.ToInt32();
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalWindowHandle(System.IntPtr ptr)
		{
			return new SalWindowHandle(ptr);
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalWindowHandle(int num)
		{
			return new SalWindowHandle(new System.IntPtr(num));
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalWindowHandle(uint num)
		{
			return new SalWindowHandle(new System.IntPtr((long)((ulong)num)));
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator SalBoolean(SalWindowHandle handle)
		{
			return !handle.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static implicit operator bool(SalWindowHandle handle)
		{
			return !handle.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator true(SalWindowHandle handle)
		{
			return !handle.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator false(SalWindowHandle handle)
		{
			return handle.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator !(SalWindowHandle handle)
		{
			return handle.IsNull;
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator ==(SalWindowHandle hWnd1, SalWindowHandle hWnd2)
		{
			return hWnd1.ToIntPtr() == hWnd2.ToIntPtr();
		}
		[System.Diagnostics.DebuggerStepThrough]
		public static bool operator !=(SalWindowHandle hWnd1, SalWindowHandle hWnd2)
		{
			return hWnd1.ToIntPtr() != hWnd2.ToIntPtr();
		}
		[System.Diagnostics.DebuggerStepThrough]
		public override bool Equals(object obj)
		{
			if (obj is SalWindowHandle)
			{
				return this.CompareTo((SalWindowHandle)obj) == 0;
			}
			if (obj == null)
			{
				return this.CompareTo(SalWindowHandle.Null) == 0;
			}
			return obj is System.Windows.Forms.Control && this.CompareTo((System.Windows.Forms.Control)obj) == 0;
		}
		public override int GetHashCode()
		{
			return this.Value.GetHashCode();
		}
		
		ulong System.IConvertible.ToUInt64(System.IFormatProvider provider)
		{
			return (ulong)this.Value.ToInt64();
		}
		sbyte System.IConvertible.ToSByte(System.IFormatProvider provider)
		{
			return System.Convert.ToSByte((uint)((int)this.Value), provider);
		}
		double System.IConvertible.ToDouble(System.IFormatProvider provider)
		{
			return System.Convert.ToDouble((uint)((int)this.Value), provider);
		}
		System.DateTime System.IConvertible.ToDateTime(System.IFormatProvider provider)
		{
			return System.Convert.ToDateTime((uint)((int)this.Value), provider);
		}
		float System.IConvertible.ToSingle(System.IFormatProvider provider)
		{
			return System.Convert.ToSingle((uint)((int)this.Value), provider);
		}
		bool System.IConvertible.ToBoolean(System.IFormatProvider provider)
		{
			return !this.IsNull;
		}
		int System.IConvertible.ToInt32(System.IFormatProvider provider)
		{
			return this.Value.ToInt32();
		}
		ushort System.IConvertible.ToUInt16(System.IFormatProvider provider)
		{
			return System.Convert.ToUInt16((uint)((int)this.Value), provider);
		}
		short System.IConvertible.ToInt16(System.IFormatProvider provider)
		{
			return System.Convert.ToInt16((uint)((int)this.Value), provider);
		}
		byte System.IConvertible.ToByte(System.IFormatProvider provider)
		{
			return System.Convert.ToByte((uint)((int)this.Value), provider);
		}
		char System.IConvertible.ToChar(System.IFormatProvider provider)
		{
			return System.Convert.ToChar((uint)((int)this.Value), provider);
		}
		long System.IConvertible.ToInt64(System.IFormatProvider provider)
		{
			return this.Value.ToInt64();
		}
		System.TypeCode System.IConvertible.GetTypeCode()
		{
			return System.TypeCode.UInt32;
		}
		decimal System.IConvertible.ToDecimal(System.IFormatProvider provider)
		{
			return System.Convert.ToDecimal((uint)((int)this.Value));
		}
		object System.IConvertible.ToType(System.Type targetType, System.IFormatProvider provider)
		{
			if (targetType == typeof(System.Windows.Forms.Control))
			{
				return this.ToControl();
			}
			if (targetType == typeof(System.IntPtr))
			{
				return this.ToIntPtr();
			}
			if (targetType == typeof(SalWindowHandle))
			{
				return this;
			}
			return System.Convert.ChangeType(this.Value, targetType, provider);
		}
		uint System.IConvertible.ToUInt32(System.IFormatProvider provider)
		{
			return (uint)this.Value.ToInt32();
		}
		string System.IConvertible.ToString(System.IFormatProvider provider)
		{
			return System.Convert.ToString((uint)((int)this.Value), provider);
		}
		public int CompareTo(object obj)
		{
			return this.CompareTo((SalWindowHandle)obj);
		}
		public int CompareTo(SalWindowHandle handle)
		{
			return this.ToInt32().CompareTo(handle.ToInt32());
		}
		public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
		{
			info.AddValue("Handle", this.Value);
		}
		
		
		public SalBoolean EndDialog(SalNumber nReturn)
		{
			return SalWindow.EndDialog(this, nReturn);
		}
		public SalWindowHandle GetDefaultButton()
		{
			return SalWindow.GetDefaultButton(this);
		}
		
		
		
		public SalBoolean MDIArrangeIcons()
		{
			return SalWindow.MDIArrangeIcons(this);
		}
		public SalBoolean MDICascade()
		{
			return SalWindow.MDICascade(this);
		}
		public SalBoolean MDITile(SalBoolean bPosition)
		{
			return SalWindow.MDITile(this, bPosition);
		}
		public SalBoolean BringWindowToTop()
		{
			return SalWindow.BringToTop(this);
		}
		public SalBoolean CenterWindow()
		{
			return SalWindow.Center(this);
		}
		
		public SalBoolean DestroyWindow()
		{
			return SalWindow.Destroy(this);
		}
		public SalBoolean DisableWindow()
		{
			return SalWindow.DisableWindow(this);
		}
		public SalBoolean EnableWindow()
		{
			return SalWindow.EnableWindow(this);
		}
		public SalWindowHandle FindChild(SalString sWindowName)
		{
			return SalWindow.FindChild(this, sWindowName);
		}
		
		public SalBoolean GetName(ref SalString sText)
		{
			return SalWindow.GetControlName(this, ref sText);
		}
		public SalString GetName()
		{
			return SalWindow.GetControlName(this);
		}
		
		public SalNumber GetControlType()
		{
			return SalWindow.GetControlType(this);
		}
		
		public SalNumber GetWindowState()
		{
			return SalWindow.GetState(this);
		}
		public SalNumber GetText(ref SalString sText, SalNumber nMaxLen)
		{
			return SalWindow.GetText(this, ref sText, nMaxLen);
		}
		public SalString GetText(SalNumber nMaxLen)
		{
			return SalWindow.GetText(this, nMaxLen);
		}
		public SalBoolean HideWindow()
		{
			return SalWindow.HideWindow(this);
		}
		public SalBoolean InvalidateWindow()
		{
			return SalWindow.Invalidate(this);
		}
		public SalBoolean IsEnabled()
		{
			return SalWindow.IsEnabled(this);
		}
		public SalBoolean IsVisible()
		{
			return SalWindow.IsVisible(this);
		}
		
		public SalWindowHandle GetParent()
		{
			return SalWindow.GetParent(this);
		}
		
		public SalBoolean PostMessage(SalNumber nMsg, SalNumber nMywParam, SalNumber nMylParam)
		{
			return SalWindow.PostMessage(this, nMsg, nMywParam, nMylParam);
		}
		public SalNumber SendMessage(SalNumber nMsg, SalNumber nMywParam, SalNumber nMylParam)
		{
			return SalWindow.SendMessage(this, nMsg, nMywParam, nMylParam);
		}
		
		public SalWindowHandle SetFocus()
		{
			return SalWindow.SetFocus(this);
		}
		
		
		public SalBoolean SetText(SalString sText)
		{
			return SalWindow.SetText(this, sText);
		}
		public SalBoolean ShowWindow()
		{
			return SalWindow.ShowWindow(this);
		}
		public SalBoolean KillTimer(SalNumber nIDEvent)
		{
			return SalWindow.KillTimer(this, nIDEvent);
		}
		public SalBoolean SetTimer(SalNumber nIDEvent, SalNumber nElapseMilliSec)
		{
			return SalWindow.SetTimer(this, nIDEvent, nElapseMilliSec);
		}
        public System.Windows.Forms.Control Control
        {
            [System.Diagnostics.DebuggerStepThrough]
            get
            {
                return this.ToControl();
            }
        }
        public System.Windows.Forms.Control ToControl()
        {
            if (this.m_value != System.IntPtr.Zero)
            {
                if (this.m_deref == null)
                {
                    
                    return null;
                }
                else
                {
                    System.Windows.Forms.Control control = (System.Windows.Forms.Control)this.m_deref.Handle.Target;
                    if (control != null && control.IsHandleCreated)
                    {
                        return control;
                    }
                }
            }
            return null;
        }
		public SalBoolean UpdateWindow()
		{
			return SalWindow.Update(this);
		}
		public SalString GetClassName()
		{
			return SalWindow.GetClassName(this);
		}
		
		public SalBoolean IsDerivedFromClass(System.Type tClass)
		{
			return SalWindow.IsDerivedFrom(this, tClass);
		}
		public SalBoolean WinHelp(SalString sFile, SalNumber nCommand, SalNumber nData, SalString sData)
		{
			return SalWindow.WinHelp(this, sFile, nCommand, nData, sData);
		}
		public void Yield()
		{
			SalWindow.Yield(this);
		}
		public SalBoolean GetFont(ref SalString sName, ref SalNumber nSize, ref SalNumber nEnhancement)
		{
			return SalWindow.GetFont(this, ref sName, ref nSize, ref nEnhancement);
		}
		public SalBoolean SetFont(SalString sName, SalNumber nSize, SalNumber nEnhancement)
		{
			return SalWindow.SetFont(this, sName, nSize, nEnhancement);
		}
		
		public SalBoolean ClearCursor(SalNumber nType)
		{
			return SalWindow.ClearCursor(this, nType);
		}
		
		public SalBoolean SetCursor(int nResourceId, SalNumber nType)
		{
			return SalWindow.SetCursor(this, nResourceId, nType);
		}
		public SalBoolean SetCursorFile(SalString sFile, SalNumber nType)
		{
			return SalWindow.SetCursor(this, sFile, nType);
		}
		public SalBoolean SetCursor(SalString sBlob, SalNumber nType)
		{
			return SalWindow.SetCursor(this, sBlob.GetBlob(), nType);
		}
		public SalBoolean SetModified(SalBoolean bSet)
		{
			return SalWindow.SetModified(this, bSet);
		}
		public SalBoolean IsModified()
		{
			return SalWindow.IsModified(this);
		}
		public SalNumber GetDataType()
		{
			return SalWindow.GetDataType(this);
		}
		public SalBoolean Clear()
		{
			return SalWindow.Clear(this);
		}
		public SalWindowHandle GetLabel()
		{
			return SalWindow.GetLabel(this);
		}
		public SalBoolean DisableWindowAndLabel()
		{
			return SalWindow.DisableWindowAndLabel(this);
		}
		public SalBoolean EnableWindowAndLabel()
		{
			return SalWindow.EnableWindowAndLabel(this);
		}
		public SalBoolean ShowWindowAndLabel()
		{
			return SalWindow.ShowWindowAndLabel(this);
		}
		public SalBoolean HideWindowAndLabel()
		{
			return SalWindow.HideWindowAndLabel(this);
		}
		public SalBoolean SetLabelText(SalString sText)
		{
			return SalWindow.SetLabelText(this, sText);
		}
		public SalNumber GetLabelText(ref SalString sText, SalNumber nMaxLength)
		{
			return SalWindow.GetLabelText(this, ref sText, nMaxLength);
		}
		public SalString GetLabelText(SalNumber nMaxLength)
		{
			return SalWindow.GetLabelText(this, nMaxLength);
		}
		public SalBoolean IsEmpty()
		{
			return SalWindow.IsEmpty(this);
		}
		public SalBoolean IsValidDateTime()
		{
			return SalWindow.IsValidDateTime(this);
		}
		public SalBoolean IsValidDecimal(SalNumber nPrecision, SalNumber nScale)
		{
			return SalWindow.IsValidDecimal(this, nPrecision, nScale);
		}
		public SalBoolean IsValidInteger()
		{
			return SalWindow.IsValidInteger(this);
		}
		public SalBoolean IsValidNumber()
		{
			return SalWindow.IsValidNumber(this);
		}
		public SalBoolean GetFormattedText(ref SalString sText, SalBoolean bFormat)
		{
			return SalWindow.GetFormattedText(this, ref sText, bFormat);
		}
		public SalString GetFormattedText(SalBoolean bFormat)
		{
			return SalWindow.GetFormattedText(this, bFormat);
		}
		public SalNumber GetFormatCode()
		{
			return SalWindow.GetFormatCode(this);
		}
		public SalBoolean SetFormatCode(SalNumber nFormat)
		{
			return SalWindow.SetFormatCode(this, nFormat);
		}
		public SalBoolean GetInputMask(ref SalString sMask)
		{
			return SalWindow.GetInputMask(this, ref sMask);
		}
		public SalString GetInputMask()
		{
			return SalWindow.GetInputMask(this);
		}
		public SalBoolean GetFormat(ref SalString sPicture)
		{
			return SalWindow.GetFormat(this, ref sPicture);
		}
		public SalString GetFormat()
		{
			return SalWindow.GetFormat(this);
		}
		public SalBoolean IsValid()
		{
			return SalWindow.IsValid(this);
		}
		public SalBoolean SetInputMask(SalString sMask)
		{
			return SalWindow.SetInputMask(this, sMask);
		}
		public SalBoolean SetFormat(SalString sPicture)
		{
			return SalWindow.SetFormat(this, sPicture);
		}
		public SalBoolean SetText(SalString sValue, SalBoolean bFormat)
		{
			return SalWindow.SetFormattedText(this, sValue, bFormat);
		}
		public SalBoolean GetTextUnmasked(ref SalString sText)
		{
			return SalWindow.GetTextUnmasked(this, ref sText);
		}
		public SalString GetTextUnmasked()
		{
			return SalWindow.GetTextUnmasked(this);
		}
		public SalBoolean Validate()
		{
			return SalWindow.Validate(this, true);
		}
		public SalBoolean Validate(SalNumber nValidate)
		{
			return SalWindow.Validate(this, nValidate == 1);
		}
		public SalBoolean SetDataLength(SalNumber length)
		{
			return SalWindow.SetDataLength(this, length);
		}
		public SalNumber GetDataLength()
		{
			return SalWindow.GetDataLength(this);
		}
		public SalBoolean IsChecked()
		{
			return SalWindow.IsChecked(this);
		}
		
	}
}
